package com.wanxi.service.impl;

import com.wanxi.dto.ResultDTO;
import com.wanxi.dto.UserDTO;
import com.wanxi.vo.UserVO;
import com.wanxi.dao.UserDao;
import com.wanxi.dao.impl.UserImpl;
import com.wanxi.entity.UserEntity;
import com.wanxi.service.UserService;
import com.wanxi.util.Md5;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultDTO add(UserVO userVO) {

        UserEntity userEntity = new UserEntity();
//        对密码进行加密？

//        BeansUtils.copy(userVO,userEntity);
//        如果想要正确的拷贝，必须要保证里面的字段名相同
        BeanUtils.copyProperties(userVO, userEntity);
        String mingwen = userEntity.getPassword();
        String anwen = Md5.encoderByMd5(mingwen);
        userEntity.setPassword(anwen);
        return ResultDTO.getResult(userDao.add(userEntity));
    }

    @Override
    public ResultDTO delete(UserVO userVO) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userVO, userEntity);
        return ResultDTO.getResult(userDao.delete(userEntity));
    }

    @Override
    public ResultDTO findById(UserVO userVO) {
//        将前端数据封装到entity
        UserEntity userEntity = new UserEntity();
//        将里面参数第一个复制到第二个参数
        BeanUtils.copyProperties(userVO, userEntity);
//        将数据库的数据封装到dto传递给前端
        userEntity = userDao.findById(userEntity);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userEntity, userDTO);
        return ResultDTO.getResult(userDTO);
    }

    @Override
    public ResultDTO findAll(UserVO userVO) {
        UserEntity userEntity = new UserEntity();

        BeanUtils.copyProperties(userVO, userEntity);
//        只有爱好不等于null的情况下，才能将爱好进行拆分成数组
        if (userEntity.getHobby() != null) {
            userEntity.setHobbys(userEntity.getHobby().split(","));
        }
        UserDTO userDTO = new UserDTO();
//        BeanUtils.copyProperties(userDao.findAll(userEntity), userDTO);
        int count = userDao.count(userEntity);
        List<UserEntity> list = userDao.findAll(userEntity);
        return ResultDTO.getResult("", count, list);
    }

    @Override
    public ResultDTO update(UserVO userVO) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userVO, userEntity);
        return ResultDTO.getResult(userDao.update(userEntity));
    }

    @Override
    public ResultDTO login(UserVO userVO) {

        UserDao userDao = new UserImpl();
        UserEntity userEntity = new UserEntity();
//        如果想要正确的拷贝，必须要保证里面的字段名相同

        BeanUtils.copyProperties(userVO, userEntity);
        String mingwen = userEntity.getPassword();
        String anwen = Md5.encoderByMd5(mingwen);
        userEntity.setPassword(anwen);

        userEntity = userDao.findByUsernameAndPassword(userEntity);
        if (userEntity == null) {
            return ResultDTO.getResult("error");
        } else {
            return ResultDTO.getResult("success");
        }

    }
}
